﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using Pg.BioMedics.SDR.Entities;
using Pg.BioMedics.SDR.Entities.Codes;
using Pg.BioMedics.SDR.Services;
using Pg.BioMedics.SDR.Web.Controls;
using Pg.BioMedics.SDR.Web.Constants;

public partial class Operations_Views_UploadAnnotationView : BaseHostedView
{
    #region Fields

    private IIndexingService indexingService;
    private Document document;

    #endregion

    #region Properties

    /// <summary>
    /// Sets indexing service instance. This prooperty is utilized by the IoC
    /// container for supplying service instance into this view.
    /// </summary>
    public IIndexingService IndexingService
    {
        set
        {
            this.indexingService = value;
        }
    }

    #endregion

    #region Event handlers

    /// <summary>
    /// Event hanlder invoked dirung view load. This method loads 
    /// all parameters supplied by the previous view. 
    /// </summary>
    /// <param name="parameters">List of parameters for this ivew</param>
    protected override void LoadParameters(object[] parameters)
    {
        if (parameters.Length == 1)
        {
            if (parameters.First() is Document)
                this.document = parameters.First() as Document;
        }
    }    

    /// <summary>
    /// Event handler invoked when user clicks register annotation link button
    /// on the control panel. This event initiates annotation registration logic
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void registerLinkButton_Click(object sender, EventArgs e)
    {
        try
        {
            // register annotation
            this.indexingService.RegisterAnnotation(this.document,
                                                    fileUpload.UploadedFile.FileName,
                                                    fileUpload.UploadedFile.Location,
                                                    Page.User.Identity.Name);

            // return to the prevous screen
            base.Return(this.document);
        }
        catch (Exception ex)
        {
            base.Navigate(DocumentsViews.ErrorView, ex);
        }
        finally
        {
            this.fileUpload.Clean();
        }
    }

    #endregion

    #region Overriden methods

    /// <summary>
    /// Saves view state. This method is being executed when 
    /// view initialization occures.
    /// </summary>
    /// <returns>State object to save</returns>
    protected override object SaveViewState()
    {
        return this.document;
    }

    /// <summary>
    /// Loads view state saved with SaveViewState method. This method loads 
    /// view state when there is a need do relaoad view state initialized during 
    /// views transition.
    /// </summary>
    /// <param name="savedState">Saved state</param>
    protected override void LoadViewState(object savedState)
    {
        this.document = savedState as Document;
    }

    #endregion
}
